using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.DomainServices.Client;
using GradeApp.Web;
using GradeApp.Web.Services;

namespace GradeApp
{
    public static class GradeAppContext
    {
        private static GradeAppDomainContext DomainContext = new GradeAppDomainContext();

        public static Tutors CurrentTutor { get; set; }
        public static Guid CurrentTutorId { get; set; }
        private static aspnet_Users CurrentAspUser { get; set; }
        private static aspnet_Membership CurrentAspMembership { get; set; }

        public static Courses CurrentCourse { get; set; }
        public static Guid CurrentCourseId { get; set; }

        public static IEnumerable<Courses> AllCourses { get; set; }

        public static event EventHandler<EventArgs> AllCoursesUpdated = delegate { };


        public static void GetCurrentUserData()
        {
            GetCurrentAspUser();
        }

        private static void GetCurrentAspUser()
        {
            if (WebContext.Current == null || WebContext.Current.User == null)
                CurrentAspUser = null;
            string userName = WebContext.Current.User.Name;
            DomainContext.Load(DomainContext.GetAspnet_UsersQuery(), op => FindCurrentUser(op, userName), null);
        }

        private static void FindCurrentUser(LoadOperation<aspnet_Users> op, string userName)
        {
            var allUsers = op.Entities;
            CurrentAspUser = allUsers.SingleOrDefault(user => user.UserName == userName);
            if (CurrentAspUser != null)
            {
                CurrentTutorId = CurrentAspUser.UserId;
                TryGetCurrentTutor();
            }
        }

        public static void TryGetCurrentTutor()
        {
            DomainContext.Load(DomainContext.GetTutorsQuery(), FindCurrentTutor, null);
        }

        private static void FindCurrentTutor(LoadOperation<Tutors> op)
        {
            var tutors = op.Entities.ToList();
            CurrentTutor = tutors.SingleOrDefault(tutor => tutor.Id == CurrentTutorId);
            if (CurrentTutor == null)
            {
                GetCurrentUserMembership();
            }
            else
            {
                TryGetAllCourses();
            }
        }

        private static void GetCurrentUserMembership()
        {
            DomainContext.Load(DomainContext.GetAspnet_MembershipQuery(), FindCurrentMembership, null);
        }

        private static void FindCurrentMembership(LoadOperation<aspnet_Membership> op)
        {
            var allMemberships = op.Entities;
            CurrentAspMembership = allMemberships.Single(membership => membership.UserId == CurrentTutorId);
            if (CurrentAspMembership != null)
            {
                CurrentTutor = new Tutors { Id = CurrentTutorId, Name = CurrentAspUser.UserName, Mail = CurrentAspMembership.Email };
                DomainContext.Tutors.Add(CurrentTutor);
                DomainContext.SubmitChanges();
                TryGetAllCourses();
            }
        }

        public static void TryGetAllCourses()
        {
            DomainContext.Load(DomainContext.GetAllCoursesForCurrentUserQuery(), FindAllCourses, null);
        }

        private static void FindAllCourses(LoadOperation<Courses> op)
        {
            AllCourses = op.Entities;
            AllCoursesUpdated(null, EventArgs.Empty);
        }
    }
}